<?php

namespace App\Admin\Controllers\news;

use App\Admin\Actions\Row\AuditActions;
use App\Models\AdminAd;
use App\Models\AdminAdPosition;
use App\Models\AdminUserGroup;
use Carbon\Carbon;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Dcat\Admin\Widgets\Card;

class AdminAdController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(AdminAd::with(['group','position']), function (Grid $grid) {
            $grid->column('id');
            $grid->column('sort')->sortable();
            $grid->column('position.name', trans('admin-ad.fields.positions_id'));
            $grid->column('group.name', trans('admin-ad.fields.user_group_id'));
            $grid->column('author');
            $grid->column('source');
            $grid->column('cover_path')->image('', 50, 50);
            $grid->column('content')->display('查看')
                ->modal(function ($modal) {
                    $card = new Card($modal -> row -> content, $modal -> row -> content);
                    return <<<HTML
                    <style>
                        .content img {
                            max-width: 100%;
                            height: auto;
                        }
                    </style>
                    <div class="content">{$card->render()}</div>
                HTML;
                });
            $grid->column('audit_status') -> using(trans('global.options.audit_status')) -> label(['info','success','danger']);
            $grid->column('status') -> switch();
            $grid->column('show_at');

            $grid->createMode('default');
            $grid->disableEditButton(false);

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions -> disableQuickEdit();
                if ( $actions->row->audit_status==0) {
                    $actions -> append(new AuditActions($actions->row -> table));
                }
            });

            $grid->selector(function (Grid\Tools\Selector $selector) {
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('position.name',trans('admin-ad.fields.positions_id')) -> width(3);
                $filter->like('group.name',trans('admin-ad.fields.user_group_id')) -> width(3);
                $filter->like('title') -> width(3);
                $filter->equal('audit_status')->select(trans('global.options.audit_status')) -> width(3);
                $filter->equal('status')->select(trans('global.options.status')) -> width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail(mixed $id) : Show
    {
        return Show::make($id, new AdminAd(), function (Show $show) {
            $show->field('id');
            $show->field('positions_id');
            $show->field('user_group_id');
            $show->field('title');
            $show->field('author');
            $show->field('source');
            $show->field('show_at');
            $show->field('cover_path');
            $show->field('mini_wechat_link');
            $show->field('pc_link');
            $show->field('content');
            $show->field('audit_status');
            $show->field('audit_response');
            $show->field('sort');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form(): Form
    {
        return Form::make(new AdminAd(), function (Form $form) {
            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableResetButton();
            $form->display('id');
            $form->column(6, function(Form $form) {
                $form->text('title')->required();
                $form->text('author')->required();
                $form->select('positions_id')->options(function (){
                    return AdminAdPosition::where('status', 1)->pluck('name as text','id')->toArray();
                })->required();
                $form->select('user_group_id')->options(function (){
                    return AdminUserGroup::pluck('name as text','id')->toArray();
                })->required();
                $form->text('source')->required();
                $form->datetime('show_at')->required()->default(Carbon::now());
                $form->switch('status')->default(1);
            });
            $form->column(6, function(Form $form) {
                $form->image('cover_path')->autoUpload()->required();
                $form->text('mini_wechat_link');
                $form->text('pc_link');
                $form->number('sort');
            });
            $form->column(12, function(Form $form) {
                $form->editor('content');
            });
        });
    }
}
